package com.baixun.gasadmin.controller;

import com.baixun.gascommon.utils.R;
import com.baixun.gasentity.dto.DeviceStatisticsDto;
import com.baixun.gasentity.dto.MonitorStatisticsDTO;
import com.baixun.gasentity.vo.AbnormalPointVo;
import com.baixun.gasentity.vo.NormalPointVo;
import com.baixun.gasentity.vo.VideoPlaybackVO;
import com.baixun.gassystem.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: 上海燃气驾驶舱
 * @description: 首页的控制层
 * @author: yesong
 **/

@RestController
@RequestMapping("/first")
@Api(tags = "上燃驾驶舱首页")
public class FirstController {
    @Autowired
    private AiAlarmService aiAlarmService;
    @Autowired
    private AlarmService alarmService;
    @Autowired
    public SysVideoMessageService sysVideoMessageService;

    @Autowired
    private AbnormalPointService abnormalPointService;

    @Autowired
    private SysMonitorStatisticsService sysMonitorStatisticsService;

    @Autowired
    private VideoPlaybackService videoPlaybackService;

    @Autowired
    private PointService pointService;

    @ApiOperation(value = "获取所有异常点位参数", notes = "查询并返回所有异常点位信息")
    @GetMapping("/getAllowedParameters")
  //  @PreAuthorize("hasAuthority('VIEW_HOMEPAGE')")
    public R<List<AbnormalPointVo>> getAllowedParameters() {
        List<AbnormalPointVo> result = abnormalPointService.getAllowedParameters();
        return R.success(result);
    }

    @ApiOperation(value = "获取所有正常点位参数", notes = "查询并返回所有正常点位信息")
    @GetMapping("/getNormalParameters")
  //  @PreAuthorize("hasAuthority('VIEW_HOMEPAGE')")
    public R<List<NormalPointVo>> getNormalParameters() {
        List<NormalPointVo> result = abnormalPointService.getNormalParameters();
        return R.success(result);
    }

    //已修改完成
    @GetMapping("/count")
 //   @PreAuthorize("hasAuthority('VIEW_HOMEPAGE')")
    @ApiOperation(value = "获取监控统计数据", notes = "返回监控的总数、已配置AI的数量和未配置AI的数量")
    public MonitorStatisticsDTO selectCount() throws Exception {
        return (MonitorStatisticsDTO) sysMonitorStatisticsService.selectCount();
    }


    @GetMapping("/device-count")
 //   @PreAuthorize("hasAuthority('VIEW_HOMEPAGE')")
    @ApiOperation(value = "根据设备类型获取数据", notes = "返回设备总数、在线设备数量和离线设备数量")
    public List<DeviceStatisticsDto> selectDeviceCount(
            @ApiParam(value ="设备类型")@Param("deviceType")String deviceType
    ) throws Exception {
        return (List<DeviceStatisticsDto>) sysMonitorStatisticsService.selectDeviceCount(deviceType);
    }

    //统计云台监控数量
    @ApiOperation(value = "获取云端数量", notes = "获取云端的数量")
    @GetMapping("/cloud")
    public R<Integer> selectCloud() {
        int cloudPoints = pointService.selectCloud();
        return R.success(cloudPoints);
    }


    //统计普通监控数量
    @ApiOperation(value = "获取普通监控数量", notes = "获取普通监控的数量")
    @GetMapping("/normal")
    public R<Integer> selectNormal() {
        int normalPoints = pointService.selectNormal();
        return R.success(normalPoints);
    }

    // 已调通
    /**
     * 获取视频播放链接
     * 根据监控点位ID、协议类型和时间范围，获取对应的视频播放链接
     *
//     * @param pointId  监控点位 ID
//     * @param protocol 协议类型
     * @param page     当前页码，默认为1
     * @param size     每页显示数量，默认为4
     * @return 返回包含视频播放链接列表的响应对象
     */
    @GetMapping("/getPlaybackLinks")
    @ApiOperation(value = "视频监控", notes = "根据监控点位ID、协议类型和时间范围，获取对应的视频播放链接")
    public R<VideoPlaybackVO> getPlaybackLinks(
//            @ApiParam(value = "监控点位ID", required = true) @RequestParam Long pointId,
//            @ApiParam(value = "协议类型", required = true) @RequestParam String protocol,
            @ApiParam(value = "当前页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false) Integer page,
            @ApiParam(value = "每页显示数量", required = false, defaultValue = "10") @RequestParam(value = "size", required = false) Integer size
    ) throws Exception {
        // 调用业务层服务获取视频播放链接分页对象
        IPage<VideoPlaybackVO> playbackLinks = videoPlaybackService.getVideoPlaybackByPointAndProtocol(page, size);

        // 返回包含分页信息的响应对象
        return R.ok(playbackLinks);
    }


    /**
     * 获取实时监控树形结构
     */

}
